<?php
App::uses('AppModel', 'Model');
/**
 * Menus Model
 *
 * @property Menus $ParentMenus
 * @property Menus $ChildMenus
 * @property Domain $Domain
 * @property Submenu $Submenu
 */
class Menus extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'name';
	public $actsAs = array('Tree');
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		"name"=>array( 
                        "unique"=>array( 
                                "rule"=>array("checkUnique", array("name", "domain_id")), 
                                "message"=>"A link with that name already exists for that institution" 
                        ) 
                ),
		'slug' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed



/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Domain' => array(
			'className' => 'Domain',
			'joinTable' => 'domains_menuses',
			'foreignKey' => 'menus_id',
			'associationForeignKey' => 'domain_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
// 			,
// 		'Submenu' => array(
// 			'className' => 'Submenu',
// 			'joinTable' => 'menuses_submenus',
// 			'foreignKey' => 'menus_id',
// 			'associationForeignKey' => 'submenu_id',
// 			'unique' => 'keepExisting',
// 			'conditions' => '',
// 			'fields' => '',
// 			'order' => '',
// 			'limit' => '',
// 			'offset' => '',
// 			'finderQuery' => '',
// 			'deleteQuery' => '',
// 			'insertQuery' => ''
// 		)
	);

	public $belongsTo = array(
		'Upload' => array(
				'className' => 'Upload',
				'foreignKey' => 'upload_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		)
	);
	
// 	public function afterSave( $options = array() ) {
// 		if(isset($this->data['Domain']['Domain'][0])){
// 			$this->Domain->id = $this->data['Domain']['Domain'][0];
// 			$this->Domain->saveField('cache',1);
// 		}
// 		return true;
// 	}
	
	// nadpisanie funkcji sprawdzajacej unikid dwoch kolumn
	function checkUnique($data, $fields) {
		$this->Domain->id = $this->data['Domain']['Domain'][0];
		$options['joins'] = array(
				array('table' => 'domains_menuses',
						'alias' => 'DomainsMenus',
						'type' => 'inner',
						'conditions' => array(
								'Menus.id = DomainsMenus.menus_id'
						)
				),
				array('table' => 'domains',
						'alias' => 'Domain',
						'type' => 'inner',
						'conditions' => array(
								'DomainsMenus.domain_id = Domain.id'
						)
				)
		);
		$options['conditions'] = array(
				'Menus.slug'=>$this->data['Menus']['slug'],
				'Domain.id'=>$this->data['Domain']['Domain'][0]
		);
		$link = $this->find('list',$options);
		
		return (count($link) <=1 ) ? true : false;
	}
	
}
